package handles

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"vehicle-mgr/cache"
	"vehicle-mgr/models"
)

func HouseAdd(c *gin.Context) {
	house_code := c.PostForm("house_code")
	house_name := c.PostForm("house_name")
	house_to := c.PostForm("house_to")
	if house_name == "" || house_code == "" || house_to == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	data := models.House{

		HouseCode: house_code,
		HouseName: house_name,
		HouseTo:   house_to,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func HouseOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_num, _ := strconv.Atoi(c.PostForm("order_num"))
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	hid, _ := strconv.Atoi(c.PostForm("hid"))
	if order_code == "" || order_num == 0 || order_price == 0 || hid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	data := models.HouseOrder{

		OrderCode:  order_code,
		OrderNum:   order_num,
		OrderPrice: order_price,
		Hid:        hid,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func HouseOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	/*
		SELECT
		  house_orders.id AS OrderId,
		  houses.house_code AS HouseCode,
		  houses.house_name AS HouseName,
		  houses.house_to AS HouseTo,
		  house_orders.order_code AS OrderCode,
		  house_orders.order_num AS OrderNum,
		  house_orders.order_price AS OrderPrice
		FROM
		  house_orders
		  LEFT JOIN houses ON houses.id = house_orders.hid
	*/

	var house []models.HouseOrderList
	redis_key := "List_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("house_orders").
			Select(
				"house_orders.id AS OrderId",
				"houses.house_code AS HouseCode",
				"houses.house_name AS HouseName",
				"houses.house_to AS HouseTo",
				"house_orders.order_code AS OrderCode",
				"house_orders.order_num AS OrderNum",
				"house_orders.order_price AS OrderPrice",
			).
			Joins("LEFT JOIN houses ON houses.id = house_orders.hid").
			Offset(offset).Limit(size).Find(&house)
		jsonData, _ := json.Marshal(house)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	}
	var total int64
	models.DB.Model(&models.HouseOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  house,
		"total": total,
	})
	return
}

func HouseOrderDetail(c *gin.Context) {
	id := c.Query("id")

	/*
		SELECT
		  house_orders.id AS OrderId,
		  houses.house_code AS HouseCode,
		  houses.house_name AS HouseName,
		  houses.house_to AS HouseTo,
		  house_orders.order_code AS OrderCode,
		  house_orders.order_num AS OrderNum,
		  house_orders.order_price AS OrderPrice
		FROM
		  house_orders
		  LEFT JOIN houses ON houses.id = house_orders.hid
	*/

	var house []models.HouseOrderList
	redis_key := "List_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("house_orders").
			Select(
				"house_orders.id AS OrderId",
				"houses.house_code AS HouseCode",
				"houses.house_name AS HouseName",
				"houses.house_to AS HouseTo",
				"house_orders.order_code AS OrderCode",
				"house_orders.order_num AS OrderNum",
				"house_orders.order_price AS OrderPrice",
			).
			Joins("LEFT JOIN houses ON houses.id = house_orders.hid").
			Where("house_orders.id", id).Find(&house)
		jsonData, _ := json.Marshal(house)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	}
	var total int64
	models.DB.Model(&models.HouseOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  house,
		"total": total,
	})
	return
}
